//
//  AppointmentYinPenViewController.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 10/16/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "AppointmentYinPenViewController.h"

#import "OrderConfirmViewController.h"

@interface AppointmentYinPenViewController ()
{
    UIScrollView * mScrollView;
    UITextView *  textView;
    UILabel * yinPenLB;
    UILabel * timeLB;
    NSMutableArray * dataArray;
    TimeView * timeView;
    
    NSString * day;
    NSString * hour;
    NSMutableArray * yinPenTimeArray;
    NSMutableArray * emplTimeArray;
}

@end

@implementation AppointmentYinPenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"下单";
    self.type = 1;

    dataArray = [[NSMutableArray alloc] init];
    yinPenTimeArray = [[NSMutableArray alloc] init];
    emplTimeArray = [[NSMutableArray alloc] init];
    
    [self initViews];
    
    [self getYinPenInfos];
    
}

#pragma mark
#pragma mark UI布局
-(void)initViews
{
    mScrollView = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
    mScrollView.contentSize = CGSizeMake(320, self.contentView.frame.size.height);
    mScrollView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:mScrollView];
    
    UIImageView * hostpitalIV = [UIImageView initImageViewRect:CGRectMake(20, 15, 280, 40) andImage:LOADIMAGE(@"inputadress@2x") andUserInteractionEnabled:YES];
    [mScrollView addSubview:hostpitalIV];
    
    UIImageView * timeIV = [UIImageView initImageViewRect:CGRectMake(20,height_y(hostpitalIV)+15, 280, 40) andImage:LOADIMAGE(@"inputtime@2x") andUserInteractionEnabled:YES];
    [mScrollView addSubview:timeIV];
    
    yinPenLB = [UILabel initLabelText:@"请选择影棚" andRect:CGRectMake(61,0,200,40) andBackGroudColor:mClearColor andTextFont:[UIFont systemFontOfSize:14.5] andTextColor:UIColorFromRGB(0x666666) andTextAlignment:NSTextAlignmentLeft];
    yinPenLB.userInteractionEnabled = YES;
    [hostpitalIV addSubview:yinPenLB];
    
    UIImageView * arrowIV = [[UIImageView alloc] initWithFrame:CGRectMake(195, 13.5f, 7.5f, 12.5f)];
    arrowIV.image = [UIImage imageNamed:@"arrow_cell"];
    [yinPenLB addSubview:arrowIV];
    
    timeLB = [UILabel initLabelText:@"请选择预约时间" andRect:CGRectMake(61,0,200,40) andBackGroudColor:mClearColor andTextFont:[UIFont systemFontOfSize:14.5] andTextColor:UIColorFromRGB(0x666666) andTextAlignment:NSTextAlignmentLeft];
    timeLB.userInteractionEnabled = YES;
    [timeIV addSubview:timeLB];
    
    
    UIView * textbgView = [UIView initViewRect:CGRectMake(20, height_y(timeIV)+20, 280, 200) andBackGroudColor:[UIColor clearColor]];
    textbgView.layer.cornerRadius = 5;
    textbgView.layer.masksToBounds = YES;
    textbgView.layer.borderWidth = 1;
    textbgView.layer.borderColor =[UIColorFromRGB(0xd4ccc5) CGColor];
    [mScrollView addSubview:textbgView];
    
    textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 260, 180)];
    textView.font = [UIFont systemFontOfSize:14.5];
    textView.textColor = UIColorFromRGB(0x6f6254);
    textView.backgroundColor = [UIColor clearColor];
    textView.editable = NO;
    [textbgView addSubview:textView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseYinPen)];
    [yinPenLB addGestureRecognizer:tap];
    
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseTime)];
    [timeLB addGestureRecognizer:tap2];
    
    UIView * footview = [UIView initViewRect:CGRectMake(0, self.view.frame.size.height-49, UI_SCREEN_WIDTH, 49) andBackGroudColor:[UIColor whiteColor]];
    [self.view addSubview:footview];
    
    UIView * line = [UIView initViewRect:CGRectMake(0, 0.5, UI_SCREEN_WIDTH, 0.5) andBackGroudColor:UIColorFromRGB(0xd4ccc5)];
    [footview addSubview:line];
    
    UIButton * suerOrChooseBtn = [UIButton initButton:CGRectMake(45,4.5, 230, 40) btnImage:LOADIMAGE(@"xiadan@2x") btnTitle:@"下单" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:16]];
    [suerOrChooseBtn addTarget:self action:@selector(suerOrchooseempl) forControlEvents:UIControlEventTouchUpInside];
    [footview addSubview:suerOrChooseBtn];
    
    
//    //自定义时间筛选控件
//    timeView = [[TimeView alloc] initWithFrame:self.contentView.frame];
//    [timeView setSouceTime:2];
//    timeView.delegate  =self;
//    [self.view addSubview:timeView];
}

#pragma mark
#pragma mark 选择影棚
-(void)chooseYinPen
{
    ChooseYingPenViewController * cvc  = [[ChooseYingPenViewController alloc] init];
    cvc.delegate  =self;
    [self.navigationController pushViewController:cvc animated:YES];
}

#pragma mark
#pragma mark 选择时间
-(void)chooseTime
{
    
    ChooseEmplTimeViewController * cvc = [[ChooseEmplTimeViewController alloc] initWithYinPenInfoArray:yinPenTimeArray];
    cvc.delegate =self;
    [self.navigationController pushViewController:cvc animated:YES];
//    if (self.type == 1){
//        timeView.yBuseDateArray = yinPenTimeArray;
//        timeView.userDateArray = emplTimeArray;
//    }else{
//        timeView.yBuseDateArray = yinPenTimeArray;
//        timeView.userDateArray = nil;
//    }
//    [timeView setTheViewShow];
    
}




#pragma mark
#pragma mark 下单或选摄影师
-(void)suerOrchooseempl
{
    if ([yinPenLB.text isEqualToString:@"请选择影棚"]){
        [XDTools showTips:@"请选择影棚" toView:self.view];
        return;
    }
    if ([timeLB.text isEqualToString:@"请选择预约时间"]){
        [XDTools showTips:@"请选择预约时间" toView:self.view];
        return;
    }
    if (!(ISLOGING)){
        loginViewController * loginVC = [[loginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    //值的存储
    //手机号
    [XDTools sharedXDTools].iphoneNumber = [[NSUserDefaults standardUserDefaults] valueForKey:kMY_USER_IPHONE];
    [XDTools sharedXDTools].adderss =  yinPenLB.text;
    //时间
    [XDTools sharedXDTools].time = timeLB.text;
    
    //用来提交的数据

    if ([hour isEqualToString:@"上午"]){
        [XDTools sharedXDTools].ShootDate = [[NSString alloc] initWithFormat:@"%@ %@",day,@"AM"];
    }else if ([hour isEqualToString:@"下午"]){
        [XDTools sharedXDTools].ShootDate = [[NSString alloc] initWithFormat:@"%@ %@",day,@"PM"];
    }else{
        [XDTools sharedXDTools].ShootDate = [[NSString alloc] initWithFormat:@"%@ %@",day,hour];
    }
    
    [XDTools sharedXDTools].ShootAddr = [[NSString alloc] initWithFormat:@"%@",yinPenLB.text];
    
    [XDTools sharedXDTools].PSID = self.psid;
    
    //跳入订单确认页
    if(ISLOGING){
        [XDTools sharedXDTools].CustomerID = [[NSUserDefaults standardUserDefaults] objectForKey:kMY_USER_ID];
        OrderConfirmViewController * ovc = [[OrderConfirmViewController alloc] init];
        [self.navigationController pushViewController:ovc animated:YES];
    }else{
        loginViewController * loginVC = [[loginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

#pragma mark
#pragma mark ChooseYingPenViewController Delegate
-(void)chooseYinPenInfo:(NSDictionary *)dict
{
    [self resetviewdatas:dict];
}

#pragma mark
#pragma mark TimeViewDelegate
-(void)chooseTimeWithDay:(NSString *)mDay hour:(NSString *)mHour second:(NSString *)mSecond
{
    timeLB.text = [NSString stringWithFormat:@"%@ %@",mDay,mHour];

    day = [[NSString alloc] initWithFormat:@"%@",mDay];
    hour = [[NSString alloc] initWithFormat:@"%@",mHour];
}

#pragma mark
#pragma mark ChooseEmplTimeViewController Delegate
-(void)chooseTheEmplTimeWithTime:(NSString *)mString amOrPm:(NSString *)aop name:(NSString *)mname
{
    timeLB.text = [NSString stringWithFormat:@"%@ %@",mString,mname];
    day = [[NSString alloc] initWithFormat:@"%@",mString];
    hour = [[NSString alloc] initWithFormat:@"%@",mname];
}

#pragma mark
#pragma mark 获取影棚信息
-(void)getYinPenInfos
{
    if (![XDTools NetworkReachable]){
        [XDTools showTips:brokenNetwork toView:self.view];
        return;
    }
    ASIHTTPRequest * request = [XDTools postRequestWithDict:@{} API:API_getPhotoStudio];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.contentView];
        NSDictionary * tmpDict = [XDTools JSonFromString:[mrequest responseString]];
        if ([tmpDict[@"status"] intValue]==1){
            [dataArray removeAllObjects];
            [dataArray addObjectsFromArray:tmpDict[@"data"]];
            [self resetviewdatas:[dataArray firstObject]];
        }else{
            [XDTools showTips:tmpDict[@"msg"] toView:self.view];
        }
    }];
    [request setFailedBlock:^{
        [XDTools hideProgress:self.contentView];
        [XDTools showTips:networkovertime toView:self.view];
    }];
    [request startAsynchronous];
    [XDTools showProgress:self.contentView];
}

#pragma mark
#pragma mark 获取摄影师时间
-(void)getEmplTime
{
    if (![XDTools NetworkReachable]){
        return;
    }
    return;
    ASIHTTPRequest * request = [XDTools postRequestWithDict:@{@"EmplID":[XDTools sharedXDTools].EmplID} API:API_getEmplTime];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        NSDictionary * tmpDict = [XDTools JSonFromString:[mrequest responseString]];
        if ([tmpDict[@"status"] intValue]==1){
            [emplTimeArray removeAllObjects];
            if (ARRAY_IS_NOT_EMPTY(tmpDict[@"data"][@"EmplUsedTime"])){
                [emplTimeArray addObjectsFromArray:tmpDict[@"data"][@"EmplUsedTime"]];
            }
        }else{
            [XDTools showTips:tmpDict[@"msg"] toView:self.view];
        }
    }];
    [request setFailedBlock:^{
        [XDTools showTips:networkovertime toView:self.view];
    }];
    [request startAsynchronous];
}

#pragma mark
#pragma mark UI赋值
-(void)resetviewdatas:(NSDictionary *)dict
{
    yinPenLB.text = dict[@"StudioName"];
    textView.text = dict[@"PSDesc"];
    
    self.psid = dict[@"PSID"];
    
    [yinPenTimeArray removeAllObjects];
    [yinPenTimeArray addObjectsFromArray:dict[@"UsedDate"]];
    
    timeLB.text = @"请选择预约时间";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
